const { jobModel, jobAdnMenuModel, jobAndBtnModel, TeaModel, StuModel } = require("../model");
//添加职位
exports.addJob = async (req, res) => {
  const { jobName, jobContent } = req.body;
  if (!jobName || !jobContent) {
    return res.status(400).json({
      code: 400,
      message: "参数不完整",
    });
  }
  let result = await jobModel.find({ jobName });
  if (result.length > 0) {
    return res.status(400).json({
      code: 400,
      message: "该职位已存在",
    });
  }
  try {
    let dbBack = new jobModel({
      jobName,
      jobContent,
    });
    await dbBack.save();
    return res.status(200).json({
      code: 200,
      data: null,
      message: "添加成功",
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "添加失败",
    });
  }
};
//给职位分配菜单权限
exports.addMenu = async (req, res) => {  
  const { jobId, menuArr } = req.body;  
  if (!jobId || !Array.isArray(menuArr)) {  
    return res.status(400).json({  
      code: 400,  
      message: "参数不完整或格式不正确",  
    });  
  }  
  
  try {  
    // 根据jobId删除所有的信息  
    await jobAdnMenuModel.deleteMany({ jobId });  
      
    // 准备要插入的新记录数组  
    const newRecords = menuArr.map(menuId => ({ jobId, MenuId: menuId }));  
      
    // 添加新的信息，使用insertMany进行批量插入  
    await jobAdnMenuModel.insertMany(newRecords);  
      
    return res.status(200).json({  
      code: 200,  
      data: null,  
      message: "添加成功",  
    });  
  } catch (error) {  
    console.error(error); // 记录错误信息到控制台或日志系统  
    return res.status(500).json({  
      code: 500,  
      data: null,  
      message: "添加失败",  
    });  
  }  
};
//获取职位对应的菜单权限
exports.getMenu = async (req,res)=>{
  const { jobId } = req.query
  if(!jobId) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'参数不完整'
    })
  }
  try {
    let result = await jobAdnMenuModel.find({jobId}).populate('MenuId','menuName menuValue').populate('jobId','jobName')
    return res.status(200).json({
      code:200,
      data:result,
      message:'获取成功'
    })
  } catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'获取失败'
    })
  }
}
//给职位分配按钮权限
exports.addButton = async (req,res)=>{
  const { jobId, buttonArr } = req.body;  
  if (!jobId || !Array.isArray(buttonArr)) {  
    return res.status(400).json({  
      code: 400,  
      data:null,
      message: "参数不完整或格式不正确"
    });  
  }
  try {
    //根据jobId删除对应的按钮权限信息
    await jobAndBtnModel.deleteMany({jobId})
    //准备要插入的新记录数组
    const newRecords = buttonArr.map(buttonId=>{
      return {
        jobId,
        btnId:buttonId
      }
    })
    //添加新的信息，使用insertMany进行批量插入
    await jobAndBtnModel.insertMany(newRecords)
    return res.status(200).json({
      code:200,
      data:null,
      message:'添加成功'
    })
  } catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'添加失败'+error
    })
  }  
}
//获取职位对应的按钮权限
exports.getButton = async (req,res)=>{
  const { jobId } = req.query
  if(!jobId) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'参数不完整'
    })
    
  }
  try {
    let result = await jobAndBtnModel.find({jobId}).populate('btnId','ButtonName ButtonValue').populate('jobId','jobName')
    return res.status(200).json({
      code:200,
      data:result,
      message:'获取成功'
    })
  } catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'获取失败'
    })
  }
}
//获取职位列表
exports.getList = async (req,res)=>{
  const { pageNo=1 , pageSize=5 } = req.query

  if(pageNo<=0 || pageSize<=0) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'参数不正确'
    })
  }
  if(typeof (pageNo*1) !== 'number' || typeof (pageSize*1) !== 'number') {
    return res.status(400).json({
      code:400,
      data:null,
      message:'类型不正确'
    })
  }
  try {
    let result = await jobModel.find().skip((pageNo-1)*pageSize).limit(pageSize)
    let total = await jobModel.find().count()
    return res.status(200).json({
      code:200,
      data:{
        list:result,
        total
      },
      message:'获取成功'
    })
  } catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'获取失败'
    })
  }
}
//获取职位详情
exports.getDetail = async (req,res)=>{
  const { id } = req.query
  if(!id) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'参数不完整'
    })
  }
  try {
    let result = await jobModel.findById(id)
    return res.status(200).json({
      code:200,
      data:result,
      message:'获取成功'
    })
  } catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'获取失败'+error
    })
  }
}
//修改职位
exports.update = async (req,res)=>{
  const { id } = req.query
  let UpdateAt = Date.now()
  const { jobName , jobContent } = req.body
  if(!id) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'参数不完整'
    })
  }
  try {
    let result = await jobModel.findByIdAndUpdate(id,{jobName,jobContent,UpdateAt},{new:true})
    return res.status(200).json({
      code:200,
      data:result,
      message:'修改成功'
    })
  } catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'修改失败'+error
    })
  }
}
//删除职位
exports.delete = async (req,res)=>{
  const { id } = req.query
  if(!id) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'参数不完整'
    })
  }
  let stu = await jobModel.findOne({_id:id})
  if(stu.jobName === '学生') {
    return res.status(400).json({
      code:400,
      data:null,
      message:'学生职位无法删除'
    })
  }
  let result = await TeaModel.findOne({jobId:id})
  let result2 = await StuModel.findOne({jobId:id})
  if(result || result2) {
    return res.status(400).json({
      code:400,
      data:null,
      message:'该职位下存在教师或学生，无法删除'
    })
  }
  try {
    await jobModel.findByIdAndDelete(id)
    await jobAdnMenuModel.deleteMany({jobId:id})
    await jobAndBtnModel.deleteMany({jobId:id})
    return res.status(200).json({
      code:200,
      data:null,
      message:'删除成功'
    })
  }catch (error) {
    return res.status(500).json({
      code:500,
      data:null,
      message:'删除失败'+error
    })
  }
}